<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>员工列表</title>

    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath());
    %>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="${APP_PATH}/js/jquery/jquery.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="${APP_PATH}/js/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    </body>


</head>
<body>

<!-- Modal模态框，用于新增加数据 -->
<div class="modal fade" id="empAddModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">添加员工</h4>
            </div>
            <div class="modal-body" id="modeAndData">

                <form class="form-horizontal">
                    <div class="form-group">
                        <label for="empName_add_input" class="col-sm-2 control-label">empName</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="empName" id="empName_add_input"
                                   placeholder="empName">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="empName_add_input" class="col-sm-2 control-label">email</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="email" id="email_add_input"
                                   placeholder="email">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="empName_add_input" class="col-sm-2 control-label">gender</label>
                        <div class="col-sm-10">

                            <label class="radio-inline">
                                <input type="radio" name="gender" id="gender1_add_input" value="1" checked="checked"> 男
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="gender" id="gender2_add_input" value="0"> 女
                            </label>

                        </div>
                    </div>

                    <div class="form-group">
                        <label for="empName_add_input" class="col-sm-2 control-label">deptName</label>
                        <div class="col-sm-4">
                            <%--提交部门id--%>
                            <select class="form-control" name="dId">

                            </select>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="save_emp_data">确定</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal模态框，用于修改数据 -->
<div class="modal fade" id="empUpdateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myUpdateModalLabel">修改员工</h4>
            </div>
            <div class="modal-body" id="modeUpdateAndData">

                <form class="form-horizontal">

                    <div class="form-group">
                        <label for="empName_add_input" class="col-sm-2 control-label">empName</label>
                        <div class="col-sm-10">
                            <p class="form-control-static" id="empName_Update_static"></p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="empName_add_input" class="col-sm-2 control-label">email</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="email" id="email_Update_input"
                                   placeholder="email">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="empName_add_input" class="col-sm-2 control-label">gender</label>
                        <div class="col-sm-10">

                            <label class="radio-inline">
                                <input type="radio" name="gender" id="gender1_Update_input" value="1" checked="checked">
                                男
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="gender" id="gender2_Update_input" value="0"> 女
                            </label>

                        </div>
                    </div>

                    <div class="form-group">
                        <label for="empName_add_input" class="col-sm-2 control-label">deptName</label>
                        <div class="col-sm-4">
                            <%--提交部门id--%>
                            <select class="form-control" name="dId">

                            </select>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="update_emp_data">确定修改</button>
            </div>
        </div>
    </div>
</div>

<%--删除数据的提示框--%>
<div class="modal fade" tabindex="-1" id="empdeleteModal" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body">
                <p>确定删除吗？&hellip;</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="delete_btn">确定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<div class="container">
    <%--标题 --%>
    <div class="row">
        <%--占用屏幕的多少。总共为12--%>
        <div class="col-md-12">
            <h1>员工列表</h1>
        </div>
    </div>

    <%--增加，删除按钮--%>
    <div class="row">
        <div class="col-md-4 col-md-offset-8">
            <button type="button" class="btn btn-success" id="emp_add_modal_btn">（新增）INSERT</button>
            <button type="button" class="btn btn-danger">（删除）DELETE</button>
        </div>
    </div>

    <%--表格数据--%>
    <div class="row">
        <table class="table table-hover" id="emp_tables">
            <thead>
            <tr>
                <th>
                    <input type="checkbox" id="check_all"/>
                </th>
                <th>ID</th>
                <th>名字</th>
                <th>性别</th>
                <th>邮箱</th>
                <th>部门名称</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
    </div>

    <%--分页--%>
    <div class="row">
        <div class="col-md-6" id="page_info_"></div>
        <div class="col-md-6" id="page_nav">

        </div>
    </div>


</div>
</body>
>
<script type="text/javascript">
    //页面加载完以后，直接发送一个ajax请求，获得页面
    $(function () {
        clike_ajax(1);
    });

    //发送ajax请求
    function clike_ajax(pn) {
        $.ajax({
            url: "${APP_PATH}/emp/emps",
            data: "pn=" + pn,
            type: "GET",
            success: function (result) {
                //表格数据函数
                emp_table(result);
                //分页信息显示函数
                emp_page_info(result);
                //分页条函数
                emp_page_nav(result);
            }
        });
    }

    //异步加载表中数据
    function emp_table(result) {
        $("#emp_tables tbody").empty();
        var rs = result.extend.pageinfo.list;
        $.each(rs, function (index, item) {
            var chechBoxTd = $("<td><input type='checkbox' class='check_item'></td>")
            var empIdTd = $("<td></td>").append(item.empId);
            var empNameTd = $("<td></td>").append(item.empName);
            var genderTd = $("<td></td>").append(item.gender == '1' ? '男' : '女');
            var emailTd = $("<td></td>").append(item.email);
            var deptNameTd = $("<td></td>").append(item.department.deptName);
            var editBtn = $("<button></button>").addClass("btn btn-success glyphicon glyphicon-pencil edit_btn").append("编辑");
            var deleteBtn = $("<button></button>").addClass("btn btn-danger glyphicon glyphicon-remove delete_btn").append("删除");
            editBtn.attr("edit-id", item.empId);
            deleteBtn.attr("delete-id", item.empId);
            $("<tr></tr>").append(chechBoxTd).append(empIdTd).append(empNameTd).append(genderTd).append(emailTd).append(deptNameTd).append(editBtn).append(deleteBtn).appendTo("#emp_tables tbody");
        });
    }

    //异步加载页码信息
    function emp_page_info(result) {
        $("#page_info_").empty();
        $("#page_info_").append("当前第" + result.extend.pageinfo.pageNum + "页,总共" + result.extend.pageinfo.pages + "页,总共" + result.extend.pageinfo.total + "条记录");

    }

    //异步加载分页条
    function emp_page_nav(result) {
        $("#page_nav").empty();
        var a_num = result.extend.pageinfo.pageNum;//当前页码
        var ul = $("<ul></ul>").addClass("pagination");

        //首页和前一页的逻辑关系
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href", "#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));

        if (result.extend.pageinfo.hasPreviousPage == false) {
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");
        }
        firstPageLi.click(function () {

            if (a_num > 1) {
                clike_ajax(1);
            }
        });
        prePageLi.click(function () {
            var a_num = result.extend.pageinfo.pageNum;
            if (a_num > 1) {
                clike_ajax(a_num - 1);
            }
        });
        ul.append(firstPageLi).append(prePageLi);

        //末页和后一页的逻辑关系
        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href", "#"));
        lastPageLi.click(function () {
            var a_b = result.extend.pageinfo.pages;
            if (a_num < a_b) {
                clike_ajax(a_b);
            }
        });
        if (result.extend.pageinfo.hasNextPage == false) {
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        }
        nextPageLi.click(function () {
            if (a_num < result.extend.pageinfo.pages) {
                clike_ajax(a_num + 1);
            }
        });

        //for循环页码显示
        $.each(result.extend.pageinfo.navigatepageNums, function (index, item) {
            var numLi = $("<li></li>").append($("<a></a>").append(item));
            if (result.extend.pageinfo.pageNum == item) {
                numLi.addClass("active");
            }
            numLi.click(function () {
                clike_ajax(item)
            });
            ul.append(numLi);
        });
        ul.append(nextPageLi).append(lastPageLi);
        var navEle = $("<nav></nav>").append(ul);
        navEle.appendTo("#page_nav");
    }


    //    增加员工
    $("#emp_add_modal_btn").click(function () {

        //调用ajax查询部门信息

        $("#empAddModal").modal({
            backdrop: "static"
        });
        dept_find("#empAddModal select");


    });

    function dept_find(ele) {
        $.ajax({
            url: "${APP_PATH}/dept/depts",
            type: "GET",
            success: function (result) {
                //处理显示页面
                dept_name(result, ele);
            }
        });
    }

    //下拉显示部门名称页面
    function dept_name(result, ele) {
        $("#empAddModal select").empty();
        $.each(result.extend.deptAll, function (index, item) {
            var optionEle = $("<option></option>").append(item.deptName).attr("value", item.deptId);
            optionEle.appendTo(ele);
        })
    }

    //给保存按钮绑定单击事件
    $("#save_emp_data").click(function () {
        $.ajax({
            url: "${APP_PATH}/emp/emps",
            type: "POST",
            data: $("#empAddModal form").serialize(),
            success: function (result) {
                // alert(result.msg);
                $("#empAddModal").modal('hide');
                clike_ajax(1);
            }
        });
    });

    //给修改按钮绑定单击事件
    //定义一个全局变量edit_id;
    var edit_id;
    $(document).on("click", ".edit_btn", function () {
        $("#empUpdateModal select").empty();
        //查出员工信息
        //显示部门列表
        $("#empUpdateModal").modal({
            backdrop: "static"
        });
        dept_find("#empUpdateModal select");
        edit_id = $(this).attr("edit-id");
        findDataById($(this).attr("edit-id"));
    });

    //通过id查询数据显示出来
    function findDataById(id) {
        $.ajax({
            url: "${APP_PATH}/emp/emps/" + id,
            type: "GET",
            success: function (result) {
                var empData = result.extend.emp;
                $("#empName_Update_static").text(empData.empName);
                $("#email_Update_input").val(empData.email);
                $("#empUpdateModal input[name=gender]").val([empData.gender]);
                $("#empUpdateModal select").val([empData.dId]);
            }
        })
    }

    //为确定更新按钮绑定单击事件
    $("#update_emp_data").click(function () {
        $.ajax({
            url: "${APP_PATH}/emp/emps/" + edit_id,
            data: $("#empUpdateModal form").serialize() + "&_method=put",
            type: "POST",
            success: function (result) {
                console.log(result);
                $("#empUpdateModal").modal('hide');
            }

        });

    });

    //删除某一个员工
    $(document).on("click", ".delete_btn", function () {

        //显示出提示框
        $("#empdeleteModal").modal({
            backdrop: "static"
        });
        delete_id = $(this).attr("delete-id");
        //为删除按钮绑定单击事件
        $("#delete_btn").click(function () {
            $.ajax({
                url: "${APP_PATH}/emp/emps/" + delete_id,
                type: "DELETE",
                success: function (result) {
                    console.log(result)
                    $("#empdeleteModal").modal('hide');
                    clike_ajax(1);
                }
            });
        });
    });

    //为全选绑定单击事件
    $("#check_all").click(function () {
        $(".check_item").prop("checked", $(this).prop("checked"));
    });
    //check_item
    $(document).on("click", ".check_item", function () {
        //判断当前选中的元素是不是五个
        var flag = $(".check_item:checked").length == $(".check_item").length;
        $("#check_all").prop("checked", flag);
    });
</script>

</html>